/*
 * This source code is part of TWaver 4.0
 *
 * Serva Software PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * Copyright 2002 - 2011 Serva Software. All rights reserved.
 */

package demo.chart.bubble;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import javax.swing.JCheckBox;
import javax.swing.JToolBar;

import twaver.Element;
import twaver.Node;
import twaver.TDataBox;
import twaver.TWaverConst;
import twaver.TWaverUtil;
import twaver.chart.Bubble;
import twaver.chart.BubbleChart;
import demo.chart.Portlet;

public class SimpleBubbleChart extends Portlet {
	private TDataBox box = new TDataBox();
	private Comparator comparator = new Comparator() {
		public int compare(Object o1, Object o2) {
			Bubble s1 = (Bubble) o1;
			Bubble s2 = (Bubble) o2;
			int value = 0;
			if (s1.getX() > s2.getX()) {
				value = 1;
			}
			if (s1.getX() < s2.getX()) {
				value = -1;
			}
			return value;
		}
	};
	private BubbleChart chart = new BubbleChart(box){
		protected List getBubbleList(Element element, List list) {
			List newList = new ArrayList(list);
			Collections.sort(newList,comparator);
			return newList;
		}
	};
	private int[] style = new int[] { 
			TWaverConst.INFLEXION_STYLE_DIAMOND, 
			TWaverConst.INFLEXION_STYLE_CIRCLE, 
			TWaverConst.INFLEXION_STYLE_RECTANGLE,
			TWaverConst.INFLEXION_STYLE_TRIANGLE };

	public SimpleBubbleChart() {
		super.initialize(chart);
		chart.setYGap(20);
		chart.setYScaleTextVisible(true);
		chart.setYScaleMaxValue(110);
		chart.setYScaleMinValue(0);
		chart.setYScaleValueGap(10);
		chart.setXScaleLineVisible(true);
		chart.getLegendPane().setVisible(false);
		chart.setBackgroundVisible(true);
		chart.setYScaleTextVisible(false);
		chart.setBackgroundGradientFactory(TWaverConst.GRADIENT_LINE_SE);
		chart.setBubbleGradientFactory(TWaverConst.GRADIENT_LINE_SE);
		chart.setValueTextVisible(false);
		initELements();
	}
	public JToolBar getControlPanel() {
		JToolBar toolbar = super.getControlPanel();
		toolbar.add(getRunButton());
		final JCheckBox x = new JCheckBox("xAxis");
		x.setPreferredSize(new Dimension(x.getPreferredSize().width + 5, 20));
		x.setSelected(chart.isXAxisVisible());
		x.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				chart.setXAxisVisible(x.isSelected());
			}
		});
		final JCheckBox y = new JCheckBox("yAxis");
		y.setPreferredSize(new Dimension(y.getPreferredSize().width + 5, 20));
		y.setSelected(chart.isYAxisVisible());
		y.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				chart.setYAxisVisible(y.isSelected());
			}
		});
		toolbar.add(x);
		toolbar.add(y);
		return toolbar;
	}
	public void run() {
		if(!isRunning()){
			return;
		}
		box.removeElement(box.getRandomElement());

		Element element = new Node();
		element.putChartColor(TWaverUtil.getRandomColor());
		element.putChartBubbleStyle(style[TWaverUtil.getRandomInt(style.length)]);
		for (int j = 1; j < 9; j++) {
			Bubble bubble = new Bubble(90 * Math.random(), 10 + 90 * Math.random(),10);
			element.addChartBubble(bubble);
		}
		box.addElement(element);
	}

	public void initELements() {
		for (int i = 0; i < style.length; i++) {
			Element element = new Node();
			element.putChartColor(TWaverUtil.getRandomColor());
			element.putChartBubbleStyle(style[i]);
			for (int j = 1; j < 9; j++) {
				Bubble bubble = new Bubble(90 * Math.random(), 10 + 90 * Math.random(),10);
				element.addChartBubble(bubble);
			}
			box.addElement(element);
		}
	}
}